package com.linchuan.concurrency;

import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
  public static void main(String[] args) throws InterruptedException {
    Timer timer = new Timer();
    TimerTask task1 =
        new TimerTask() {
          @Override
          public void run() {
            System.out.println("Task1" + System.currentTimeMillis());
          }
        };
    TimerTask task2 =
        new TimerTask() {
          @Override
          public void run() {
            System.out.println("Task2" + System.currentTimeMillis());
            try {
              Thread.sleep(1900);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        };

    //    timer.scheduleAtFixedRate(task1, 0, 1000);
    //    timer.scheduleAtFixedRate(task2, 0, 2000);
    timer.schedule(new ThrowTask(), 1);
    Thread.sleep(10000);
    timer.schedule(new ThrowTask(), 1);
    Thread.sleep(30000);
  }
}

class ThrowTask extends TimerTask {

  @Override
  public void run() {
    System.out.println(this.getClass().getSimpleName());
    throw new RuntimeException();
  }
}
